angular.module('charitySociety.service', []).service('$charitySocietyService', ['$http', function ($http) {

    var NUM_PER_PAGE = 10;
    /**
     * 查询文章明细
     * @param param
     * @param success
     * @param fail
     */
    this.getArticleDetail = function (param, success, fail) {
        $http({
            method: 'GET',
            url: '/articles/',
            params: param
        }).then(success, fail);
    };
    /**
     * 分页查询文章列表
     * @param pageNo
     * @param success
     * @param fail
     */
    this.getArticles = function (param, success, fail) {
        var numPerPage = NUM_PER_PAGE;
        if(param.numPerPage != undefined  && param.numPerPage > 0){
            numPerPage = param.numPerPage;
        }
        var from = param.pageNo * numPerPage;
        $http({
            method: 'GET',
            url: '/articles/' + from + '/'+numPerPage,
            params: param
        }).then(success, fail);
    }

    /**
     * 按年份分页查询文章列表
     * @param pageNo
     * @param success
     * @param fail
     */
    this.getArticlesByYear = function (param, success, fail) {
        var numPerPage = NUM_PER_PAGE;
        if(param.numPerPage != undefined  && param.numPerPage > 0){
            numPerPage = param.numPerPage;
        }
        var from = param.pageNo * numPerPage;
        var year  = param.year;
        $http({
            method: 'GET',
            url: '/articles/'+year+'/'+ from + '/'+numPerPage,
            params: param
        }).then(success, fail);
    }

    this.getEventsData = function(success,fail){
        $http({
            method: 'GET',
            url: '/charityEventData'
        }).then(success, fail);
    }

    this.getCurrentYearDonation = function () {
        return $http({
            method: 'GET',
            url: '/dataAnalysis/yearDonation'
        }).then(function (result) {
            return result.data;
        });
    }

    this.getCurrentMonthDonation = function () {
        return $http({
            method: 'GET',
            url: '/dataAnalysis/monthDonation'
        }).then(function (result) {
            return result.data;
        });
    }

    this.getAllDonation = function () {
        return $http({
            method: 'GET',
            url: '/dataAnalysis/allDonation'
        }).then(function (result) {
            return result.data;
        });
    }
    this.getLatestDonations = function () {
        return $http({
            method: 'GET',
            url: '/dataAnalysis/latestDonations'
        }).then(function (result) {
            return result.data;
        });
    }
    this.getHeadBannerSources = function () {
        return $http({
                method: 'GET',
                url: "/banners/headBanner"
            }
        ).then(function (result) {
            return result.data;
        })
    }
}]);